<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>工具</title>

<!--  Add to header -->

<script language="JavaScript">

<!--
function Tools(){};
Tools.prototype.t1 = function(string) {
	var newV = '';
	var reg = /\s*(\w+)\s+\.*/;
	reg.exec(string);
	var oldV = RegExp.$1;
	return  oldV;
}

Tools.prototype.t2 = function (string) {
	var newV = '';
	var reg = /\s*(\w+)\s+\.*/;
	reg.exec(string);
	var oldV = RegExp.$1;
	var desc;
	var decArr;
	decArr = string.split("	"); 
	if(decArr.length>1){
		desc=decArr[decArr.length-1];
	}else{
		decArr = string.split("Y      "); 
		if(decArr.length>1){
			desc=decArr[decArr.length-1];
		}else{
			var decArr = string.split("N      "); 
			if(decArr.length>1){
				desc=decArr[decArr.length-1];
			}
		}
	}
	return  oldV+","+desc;
}


Tools.prototype.t3 = function (string) {
	var newV = '';
	var reg = /\s*(\w+)\s+\.*/;
	reg.exec(string);
	var oldV = RegExp.$1;
	var varray = oldV.split("_");
	for(var i=0;i<varray.length;i++){
		if(i==0){
			newV += varray[i].toLocaleLowerCase();
		}else{
			var temp = varray[i].substr(0,1) + varray[i].substr(1).toLocaleLowerCase();
			newV += temp;
		}
	}
	//var sub=document.getElementById("sub").value;
	//var decArr = string.split(sub);
	var desc;
	var decArr;
	decArr = string.split("	"); 
	if(decArr.length>1){
		desc=decArr[decArr.length-1];
	}else{
		decArr = string.split("Y      "); 
		if(decArr.length>1){
			desc=decArr[decArr.length-1];
		}else{
			
			//var decArr = string.split("N      "); 
			var decArr = string.replace(/\s+/g,",")
			decArr = decArr.split(","); 
			if(decArr.length>1){
				desc=decArr[decArr.length-1];
			}
		}
	}
	desc="/**\n"
			+"  * "+desc+"\n"
			+"  */\n";
	return  desc+"private String "+newV+";";
}

Tools.prototype.t4 = function (string) {
	var newV = '';
	var reg = /\s*(\w+)\s+\.*/;
	reg.exec(string);
	var oldV = RegExp.$1;
	var varray = oldV.split("_");
	for(var i=0;i<varray.length;i++){
		if(i==0){
			newV += varray[i].toLocaleLowerCase();
		}else{
			var temp = varray[i].substr(0,1) + varray[i].substr(1).toLocaleLowerCase();
			newV += temp;
		}
	}
	//var sub=document.getElementById("sub").value;
	//var decArr = string.split(sub);
	var desc;
	var decArr;
	decArr = string.split("	"); 
	if(decArr.length>1){
		desc=decArr[decArr.length-1];
	}else{
		decArr = string.split("Y      "); 
		if(decArr.length>1){
			desc=decArr[decArr.length-1];
		}else{
			//var decArr = string.split("N      "); 
			var decArr = string.replace(/\s+/g,",")
			decArr = decArr.split(","); 
			if(decArr.length>1){
				desc=decArr[decArr.length-1];
			}
		}
	}
	return  "private String "+newV+";"+"//"+desc;
}


Tools.prototype.t5 = function (string) {
	var newV = '';
	var reg = /\s*(\w+)\s+\.*/;
	reg.exec(string);
	var oldV = RegExp.$1;
	return  oldV;
}

function change(flag){
	var tools = new Tools();
	var newString = '';
	var param = '';
	var string = document.getElementById("source").value;
	var strings = string.split("\n");
	for(var i=0;i<strings.length;i++){
		if(flag==1){
			var temp = tools.t1(strings[i]);
			temp+="                   ";
			temp="       "+temp.substring(0,25)+" = ?,\n";
		}else if(flag==2){
			var temp = tools.t2(strings[i]) + ",";
		}else if(flag==3){
			var temp = tools.t3(strings[i]) + "\n";
		}else if(flag==4){
			var temp = tools.t4(strings[i]) + "\n";
		}else if(flag==5){
			var temp = "    "+tools.t5(strings[i]) + ",\n";
			param+='?,'
		}else if(flag==6){
			var temp = "       "+tools.t5(strings[i]) + ",\n";
		}
		newString += temp;
	}
	var tableName=document.getElementById("tableName").value;//表名
	var keyName=document.getElementById("keyName").value;//主键名称
	if(flag==5){
		var column = document.getElementById("column");
		var len = column.options.length;
		if(len>0){
			//生成插入语句
			 newString=""; 
			 param="";
			 for (var i = 0; i < column.options.length; i++) {        
				var temp = "    "+column.options[i].value  + ",\n";
				param+='?,'
				newString += temp;
			}       
			
		}
		//生成插入语句
		newString=newString.substring(4,newString.length-2);
		
		if(tableName==''){
			alert('请输入表名!');
			newString='';
		}else{
			param=param.substring(0,param.length-1);
			newString="insert into "+tableName+"\n"
					 +"   ("+newString+")\n"
					 +"values\n"
					 +"("+param+")";
		}
	
	}
	
	if(flag==2){
		//生成删除语句
		if(tableName==''){
			alert('请输入表名!');
			newString='';
		}else{
			newString="";
			var column = document.getElementById("column");
			var len = column.options.length;
			if(len>0){
				var columnDelete = document.getElementById("columnDelete");
				var delLen = columnDelete.options.length;
				//如果只是删除部分字段
				if(delLen>0){
					 newString+='delete ';
					 for (var i = 0; i < column.options.length; i++) {        
						var temp = column.options[i].value  + ",";
						newString += temp;
					}   
					newString=newString.substring(0,newString.length-1);
					newString+=" from "+tableName;//删除表
					
				}else{
					 newString+='delete from '+tableName;//删除表
				}
				var where = document.getElementById("where");
				var whereLen = where.options.length;
				if(whereLen>0){
				    var whereSql="";
					for (var i = 0; i < where.options.length; i++) {        
						var temp = " AND "+where.options[i].value  + " = ?";
						whereSql += temp;
					}  
					if(whereSql.length>0){
						newString+=" where "+whereSql.substring(4,whereSql.length);
					}
				}
				
			}else{
				if(keyName==''){
					newString="delete from "+tableName;
				}else{
					newString="delete from "+tableName +" where "+keyName +" = ?";
				}
			}
		}
		
	}
	
	if(flag==1){
		//生成修改语句
		if(tableName==''){
			alert('请输入表名!');
			newString='';
		}else{
			
			var column = document.getElementById("column");
			var len = column.options.length;
			if(len>0){
				newString="update "+tableName+"\n";
				var updateSql="";
				for (var i = 0; i < column.options.length; i++) {        
					var temp =  column.options[i].value;
						temp+="                   ";
						temp="       "+temp.substring(0,25)+" = ?,\n";
					updateSql += temp;
				}   
				newString+="   set "+updateSql.substring(7,updateSql.length-2)+"\n";
				var where = document.getElementById("where");
				var whereLen = where.options.length;
				if(whereLen>0){
				    var whereSql="";
					for (var i = 0; i < where.options.length; i++) {        
						var temp = " AND "+where.options[i].value  + " = ?";
						whereSql += temp;
					}  
					if(whereSql.length>0){
						newString+=" where "+whereSql.substring(4,whereSql.length);
					}
				}
			
			}else{
				if(keyName==''){
					newString="update "+tableName+"\n"
							 +"   set "+newString.substring(7,newString.length-2);
				}else{
					newString="update "+tableName+"\n"
							 +"   set "+newString.substring(7,newString.length-2)+"\n"
							 +" where "+keyName+" = ?";
				}
			}
		}
		
	}
	
	
	
	if(flag==6){
		//生成查询语句
		if(tableName==''){
			alert('请输入表名!');
			newString='';
		}else{
			
			var column = document.getElementById("column");
			var len = column.options.length;
			if(len>0){
				newString="select ";
				var selcetSql="";
				for (var i = 0; i < column.options.length; i++) {        
					var temp =  column.options[i].value;
						temp="       "+temp+",\n";
					selcetSql += temp;
				}   
				newString+=selcetSql.substring(7,selcetSql.length-2)+"\n"
						 +"  from "+tableName+"\n";
				var where = document.getElementById("where");
				var whereLen = where.options.length;
				if(whereLen>0){
				    var whereSql="";
					for (var i = 0; i < where.options.length; i++) {        
						var temp = " AND "+where.options[i].value  + " = ?";
						whereSql += temp;
					}  
					if(whereSql.length>0){
						newString+=" where "+whereSql.substring(4,whereSql.length);
					}
				}
			
			}else{
				if(keyName==''){
					newString="select "+newString.substring(7,newString.length-2)+"\n"
							 +"  from "+tableName;
				}else{
					newString="select "+newString.substring(7,newString.length-2)+"\n"
							 +"  from "+tableName+"\n"
							 +" where "+keyName+" = ?";
				}
			}
		}
		
	}
	
	
	document.getElementById("dest").value = newString;
}
//-->

 function changeOne(){
	dest.value=".append(\""+source.value.split('\r\n').join("\")\n.append(\"")+"\")";
 } 

  function changeTow(){
        var sub="+\""+source.value.split('\r\n').join("\"\n+\"")+"\"";
        sub=sub.substr(1,sub.length);
		dest.value=sub;
 } 
 function changeGeneral(){
	var aa= document.getElementById("source").value;
	var array=aa.split('\r\n');
	var sql="";
	for(var i=0;i<array.length;i++){
		var first=array[i].indexOf("\"")+1;
		var last=array[i].lastIndexOf("\"");
		sql+=array[i].substring(first,last)+"\n";
	}
	document.getElementById("dest").value=sql;
 } 
 
 function create(){
 	var tools = new Tools();
	var string = document.getElementById("source").value;
	var column = document.getElementById("column");
	var columnDelete = document.getElementById("columnDelete");
	var whereDelete = document.getElementById("whereDelete");
	var where = document.getElementById("where");
	column.options.length = 0;  
	where.options.length = 0;  
	columnDelete.options.length = 0; 
	whereDelete.options.length = 0; 
	var strings = string.split("\n");
	for(var i=0;i<strings.length;i++){
		var temp = tools.t1(strings[i]);
		var varItem = new Option(temp, temp);     
		var whereItem = new Option(temp, temp);       
        column.options.add(varItem);
		whereDelete.options.add(whereItem);
	}
 } 
 
 function createCN(){
 	var tools = new Tools();
	var string = document.getElementById("source").value;
	var column = document.getElementById("column");
	var columnDelete = document.getElementById("columnDelete");
	var whereDelete = document.getElementById("whereDelete");
	var where = document.getElementById("where");
	column.options.length = 0;  
	where.options.length = 0;  
	columnDelete.options.length = 0; 
	whereDelete.options.length = 0; 
	var strings = string.split("\n");
	for(var i=0;i<strings.length;i++){
		var temp = tools.t2(strings[i]);
		var tempArr=temp.split(",");
		var varItem = new Option(tempArr[1], tempArr[0]);     
		var whereItem = new Option(tempArr[1], tempArr[0]);       
        column.options.add(varItem);
		whereDelete.options.add(whereItem);
	}
 } 
  

 function removeLeft(){
 	var column = document.getElementById("column");
	var columnDelete = document.getElementById("columnDelete");
 	var length = column.options.length - 1;    
    for(var i = length; i >= 0; i--){    
        if(column[i].selected == true){    
			var varItem = new Option(column.options[i].text, column.options[i].value);
			columnDelete.options.add(varItem);
            column.options[i] = null;    
        }    
    }  
 }
 
  function removeRigth(){
 	var column = document.getElementById("column");
	var columnDelete = document.getElementById("columnDelete");
 	var length = columnDelete.options.length - 1;    
    for(var i = length; i >= 0; i--){    
        if(columnDelete[i].selected == true){    
			var varItem = new Option(columnDelete.options[i].text, columnDelete.options[i].value);
			column.options.add(varItem);
            columnDelete.options[i] = null;    
        }    
    }  
 }
 
 
  function removeWhereLeft(){
 	var column = document.getElementById("where");
	var columnDelete = document.getElementById("whereDelete");
 	var length = column.options.length - 1;    
    for(var i = length; i >= 0; i--){    
        if(column[i].selected == true){    
			var varItem = new Option(column.options[i].text, column.options[i].value);
			columnDelete.options.add(varItem);
            column.options[i] = null;    
        }    
    }  
 }
 
  function removeWhereRigth(){
 	var column = document.getElementById("where");
	var columnDelete = document.getElementById("whereDelete");
 	var length = columnDelete.options.length - 1;    
    for(var i = length; i >= 0; i--){    
        if(columnDelete[i].selected == true){    
			var varItem = new Option(columnDelete.options[i].text, columnDelete.options[i].value);
			column.options.add(varItem);
            columnDelete.options[i] = null;    
        }    
    }  
 }

</script>
</head>
<body>
	<textarea id="source" rows="15" cols="150"></textarea>
	<br>
	<button onclick="change(4)">数据库声明语句转换JavaBean普通注释</button>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<button onclick="change(3)">数据库声明语句转换JavaBean文档注释</button>
	<br>
	<div style="height: 150px;">
		<span>列名:</span> <select size="8" multiple id="columnDelete">

		</select>
		<button onclick="removeLeft()"><<</button>
		<button onclick="removeRigth()">>></button>
		<select size="8" multiple id="column">

		</select> <span>where子句:</span> <select size="8" multiple id="whereDelete">

		</select>
		<button onclick="removeWhereLeft()"><<</button>
		<button onclick="removeWhereRigth()">>></button>
		<select size="8" multiple id="where">

		</select>
	</div>
	<button onclick="create()">生成列表信息(英文字段)</button>
	<button onclick="createCN()">生成列表信息(中文注释)</button>
	<br> 表名:
	<input id="tableName" size="30" text="" /> 主键名称:
	<input id="keyName" size="30" text="" />
	<br>
	<button onclick="change(5)">生成数据库insert语句</button>
	&nbsp;&nbsp;
	<button onclick="change(1)">生成数据库update语句</button>
	&nbsp;&nbsp;
	<button onclick="change(2)">生成数据库delete语句</button>
	&nbsp;&nbsp;
	<button onclick="change(6)">生成数据库select语句</button>
	&nbsp;&nbsp;
	<br> 将SQL语句转换JAVA字符串:
	<button onclick="changeOne()">使用append</button>
	<button onclick="changeTow()">使用+</button>
	将JAVA字符串转换SQL语句:
	<button onclick="changeGeneral()">转化普通sql</button>
	<br>
	<textarea id="dest" rows="15" cols="150"></textarea>
	<br>
</body>
</html>